package cn.wolfcode.ssm.controller;

import cn.wolfcode.ssm.domain.Classinfo;
import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.qo.ClassinfoQueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.service.IClassinfoService;
import cn.wolfcode.ssm.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {

    @Autowired
    private IClassinfoService classinfoService;
    @Autowired
    private IEmployeeService employeeService;

   @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")ClassinfoQueryObject qo) {
        PageInfo<Classinfo> pageInfo = classinfoService.selctForPageResult(qo);
       List<Employee> employees = employeeService.getAll();
       model.addAttribute("pageInfo", pageInfo);
       model.addAttribute("employees", employees);
        return "classinfo/list";
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
            if (id != null) {
                classinfoService.delete(id);
            }
            return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo) {
            if (classinfo.getId() != null) {
                classinfoService.update(classinfo);
            } else {
                classinfoService.save(classinfo);
            }
            return new JsonResult();
    }

}
